<template>
  <el-form v-bind="$attrs" @validate="formValidate">
    <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '名称不能为空' }]">
      <el-input
        v-model="formData.name"
        :maxlength="50"
        placeholder="请输入数据标题，不超过50个字符"
      />
    </el-form-item>
    <el-form-item label="资源描述" prop="desc">
      <el-input
        v-model="formData.desc"
        type="textarea"
        :rows="1"
        autosize
        :maxlength="200"
        placeholder="请输入数据的描述信息，不超过200个字符"
      />
    </el-form-item>
    <el-form-item label="">
      <div class="row-item">
        <div class="row-item-col column">
          <div class="title">样式类型</div>
          <div class="content">
            <el-radio-group
              v-model="divAttrData.attr.styleType"
              @change="specialFormValidate('styleType')"
            >
              <el-radio label="2d">二维</el-radio>
              <el-radio label="3d">三维</el-radio>
            </el-radio-group>
          </div>
        </div>
      </div>
      <div class="row-item">
        <div class="row-item-col column">
          <div class="title">不透明度</div>
          <div class="content">
            <el-slider
              class="progress"
              v-model="divAttrData.attr.alpha"
              :min="0"
              :max="100"
              :step="1"
              @change="specialFormValidate('alpha')"
            />
            <el-input
              class="progress-input"
              v-model.number="divAttrData.attr.alpha"
              max="100"
              min="0"
              @keyup.enter.native="specialFormValidate('alpha')"
            />
            <span>%</span>
          </div>
        </div>
      </div>
    </el-form-item>
    <el-collapse v-model="activeNames">
      <el-collapse-item title="尺寸" name="1">
        <el-form-item label="">
          <div class="row-item">
            <div class="title">宽度</div>
            <el-input-number
              v-model="divAttrData.attr.width"
              placeholder=""
              :controls="false"
              :precision="2"
              :min="0"
              @keyup.enter.native="specialFormValidate('width')"
            />
            <div class="unit">PX</div>
          </div>
          <div class="row-item">
            <div class="title">高度</div>
            <el-input-number
              v-model="divAttrData.attr.height"
              placeholder=""
              :controls="false"
              :precision="2"
              :min="0"
              @keyup.enter.native="specialFormValidate('height')"
            />
            <div class="unit">PX</div>
          </div>
        </el-form-item>
      </el-collapse-item>
      <el-collapse-item v-if="divAttrData.attr.styleType === '3d'" title="位置" name="2">
        <el-form-item label="">
          <div class="row-item">
            <div class="title">经度</div>
            <el-input-number
              v-model="divAttrData.attr.position[0]"
              placeholder=""
              :controls="false"
              :min="-180"
              :max="180"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">°</div>
          </div>
          <div class="row-item">
            <div class="title">纬度</div>
            <el-input
              v-model="divAttrData.attr.position[1]"
              placeholder=""
              :controls="false"
              :min="-90"
              :max="90"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">°</div>
          </div>
          <div class="row-item">
            <div class="title">高度</div>
            <el-input-number
              v-model="divAttrData.attr.position[2]"
              placeholder=""
              :controls="false"
              :precision="2"
              @keyup.enter.native="specialFormValidate('position')"
            />
            <div class="unit">米</div>
          </div>
        </el-form-item>
      </el-collapse-item>
    </el-collapse>
  </el-form>
</template>

<script setup lang="ts">
import { messageUtil } from '@jl/element-plus/utils';
import { addPanelTag, destroyLayer } from '@/service';
import { ValidateRule } from '@/views/enum/common';
import { notNumberInRange } from '@/utils';
import { updateDom } from '@/service/effect';
import { cloneDeep } from 'lodash';

defineOptions({
  name: '',
});

interface IProps {
  form: any;
}

const props = withDefaults(defineProps<IProps>(), {
  form: {},
});

const formData = ref<any>(null);
const divAttrData = ref<any>(null);
const emits = defineEmits(['formValidate']);
const activeNames = ref<string[]>(['1', '2']);

function formValidate(fieldName: string, valid: boolean) {
  emits('formValidate', fieldName, valid);
}

function specialFormValidate(fieldName: string) {
  let valid = true;
  if (['desc'].includes(fieldName)) {
    if (formData.value[fieldName].length > 200) {
      valid = false;
    }
  }
  if (['position', 'translation', 'rotation'].includes(fieldName)) {
    formData.value.attr[fieldName].forEach((list: any, index: number) => {
      if (list === '' || isNaN(list)) {
        valid = false;
      }
      const min = ValidateRule[fieldName][index].min;
      const max = ValidateRule[fieldName][index].max;
      if (min && notNumberInRange(list, min, max)) {
        valid = false;
      }
    });
  } else if (['scale'].includes(fieldName)) {
    if (formData.value.attr.scale === '' || isNaN(formData.value.attr.scale)) {
      valid = false;
    }
  } else if (['alpha'].includes(fieldName)) {
    if (formData.value.attr.alpha === '' || isNaN(formData.value.attr.alpha)) {
      valid = false;
    }
  }
  if (!valid) {
    messageUtil.warning('校验错误');
  } else {
    // 删除后重新创建dom
    if (['alpha', 'width', 'height', 'styleType'].includes(fieldName)) {
      const domData = cloneDeep(divAttrData.value);
      formData.value.attr.alpha = domData.attr.alpha;
      formData.value.attr.width = domData.attr.width;
      formData.value.attr.height = domData.attr.height;
      formData.value.attr.styleType = domData.attr.styleType;
      updateDom(domData);
      return;
    }
    destroyLayer(props.form);
    addPanelTag({
      longitude: Number(divAttrData.value.attr.position[0]),
      latitude: Number(divAttrData.value.attr.position[1]),
      height: Number(divAttrData.value.attr.position[2]),
      ...props.form,
    });
    formData.value.attr.position = cloneDeep(divAttrData.value.attr.position);
  }
  emits('formValidate', fieldName, valid);
}

watchEffect(() => {
  formData.value = props.form;
  divAttrData.value = cloneDeep(props.form);
});
</script>

<style lang="less" scoped>
.el-form {
  &-item {
    .row-item {
      width: 100%;
      display: flex;
      margin-bottom: 10px;
      .title {
        flex: 0 0 40px;
      }
      .el-input,
      .el-input-number {
        flex: 1;
      }
      .unit {
        flex: 0 0 24px;
        text-align: center;
      }
      &:last-child {
        margin-bottom: 0px;
      }
      .progress-input {
        flex: 0 0 48px;
        margin: 0 4px 0 10px;
      }
    }
  }
}
</style>
